package com.icss.ah.custMsg.action;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.j2ee.util.UUID;

/**
 * 消费者留言回复管理Action
 * @author sw
 * @date 2016-10-20
 *
 */
@Controller
@Scope("prototype")
public class CustMessageAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	// json串
	private Json jsonText;
	
	private String shopId;
	private String messageId;
	private String replyContent;
	private String type;
	
	/**
	 * 消费者留言列表页面
	 * @return
	 */
	public String toCustMsgList(){
		return "toCustMsgList";
	}
	
	/**
	 * 查询消费者留言列表
	 */
	public void queryCustMsgList(){
		request = this.getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String beginDate = request.getParameter("beginDate");
		beginDate = (beginDate == null || "".equals(beginDate)) ? "" : beginDate;
		String endDate = request.getParameter("endDate");
		endDate = (endDate == null || "".equals(endDate)) ? "" : endDate;
		String queryStatus = request.getParameter("status");
		queryStatus = (queryStatus == null || "".equals(queryStatus)) ? "" : queryStatus;
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("userCode", userCode);
			HashMap accountShop = shopService.queryAccountMap(map);
			map.put("shopId", accountShop.get("SHOP_ID") == null ? "" : accountShop.get("SHOP_ID").toString());
			map.put("corpId",corpId);	//公众号id
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("queryStatus", queryStatus);
			map.put("msgTypeCd", "1");
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> custMsgList = new ArrayList();
			custMsgList = custMessageService.queryCustMsgList(map);
			System.out.println("custMsgList--------------------------------"+custMsgList);
			long custMsgCount = custMessageService.queryCustMsgCount(map);//查询总记录数
			dataGridJson.setRows(custMsgList);
			dataGridJson.setTotal(custMsgCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("CustMessage|公众号id：" + corpId + "，查询消费者留言列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CustMessage|查询消费者留言列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	public String toReplyPage(){
		request = this.getRequest();
		String returnPage = "toReplyPage";
		if("show".equals(type)){
			returnPage = "toReplyInfo";
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("messageId", messageId);
		Map messageMap = custMessageService.queryMessageMap(map);
		request.setAttribute("messageMap", messageMap);
		request.setAttribute("messageId", messageId);
		return returnPage;
	}
	
	/**
	 * 保存留言回复信息
	 * @return
	 */
	public String saveReply() {
		request = this.getRequest();
		jsonText = new Json();
		boolean result = false;
		String sMsg = "";
		String fMsg = "";
		try {
			String userCode = getUserCodeFromSession();
			
			HashMap map = new HashMap();
			map.put("replyId", new UUID().toString());
			map.put("messageId",messageId);
			map.put("corpId",getBaseCorpId());
			map.put("userCode",userCode);
			map.put("shopId",shopId);
			map.put("replyContent", replyContent);
			result = custMessageService.saveReply(map);
			sMsg = "回复成功！";
			fMsg = "回复失败！";
			
			System.out.println("==========map:"+map);
			
			jsonText.setSuccess(result);
			if(result){
				jsonText.setMsg(sMsg);
			} else {
				jsonText.setMsg(fMsg);
			}
			infolog.info("CustMessage|saveReply|result|"+result);
		}catch (Exception e) {
			jsonText.setMsg(fMsg);
			jsonText.setSuccess(false);
			errlog.error("CustMessage|保存留言回复失败：",e);
		}
		return "jsonText";
	}
	
	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getReplyContent() {
		return replyContent;
	}

	public void setReplyContent(String replyContent) {
		this.replyContent = replyContent;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}
